package douaPahare.operator;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import douaPahare.container.Stare;


public class T21 extends JButton implements Operator {
	
    private JPanel panel;
	
	public T21(String nume,JPanel p) {
		super(nume);
		panel = p;
	}

	public void executa() throws Exception {
	    if(evaluare(Stare.getStareStatic()))
	    	Stare.setStareStatic(actiune(Stare.getStareStatic()));
		else throw new Exception("Nu se poate transfera din al doilea pahar in primul pahar!");
	}
	
	public boolean evaluare(Stare st) {
		if(st.getX() == 3 || st.getY() == 0) 
			return false;
		else return true;
	}
	
	public Stare actiune(Stare st) {
		final JProgressBar p1 = (JProgressBar)panel.getComponent(0);
		final JProgressBar p2 = (JProgressBar)panel.getComponent(2);
		if((st.getY() - (3 - st.getX())) <= 0) { // (ce ramane in paharul 2) se toarna tot paharul 2 in paharul 1
			p1.setValue(st.getX() + st.getY());
			p2.setValue(0);	
			return new Stare(st.getX() + st.getY(),0);
		} else { 
			p1.setValue(st.getX() + (3 - st.getX()));
			p2.setValue(st.getY() - (3 - st.getX()));
			return new Stare(st.getX() + (3 - st.getX()),st.getY() - (3 - st.getX()));
		}
	}
	
	public Stare actiune2(Stare st) {
		if((st.getY() - (3 - st.getX())) <= 0) { // (ce ramane in paharul 2) se toarna tot paharul 2 in paharul 1
			return new Stare(st.getX() + st.getY(),0);
		} else { 
			return new Stare(st.getX() + (3 - st.getX()),st.getY() - (3 - st.getX()));
		}
	}
	

}
